# 10.1.1 读取整个文件
with open('pi_digits.txt') as file_object:
    contents = file_object.read()
    print(contents)

# 10.1.2 文件路径
# 在Linux和OS X中
with open('text_files/pi_digits.txt') as file_object:
    contents = file_object.read()
    print(contents)
    # file_path = '/home/ehmatthes/other_files/text_files/filename.txt'
    # with open(file_path) as file_object:
# 在Windows中
with open('text_files\pi_digits.txt') as file_object:
    contents = file_object.read()
    print(contents)
    # file_path = 'C:\Users\ehmatthes\other_files\text_files\filename.txt'
    # with open(file_path) as file_object:
# 10.1.3 逐行读取
file_name = 'pi_digits.txt'
with open(file_name) as file_object:
    for line in file_object:
        print(line.rstrip())
# 10.1.4 创建一个包含文件各行内容的列表
file_name = 'pi_digits.txt'
with open(file_name) as file_object:
    lines = file_object.readlines()
for line in lines:
    print(line.rstrip())
# 10.1.5 使用文件的内容
file_name = 'pi_digits.txt'
with open(file_name) as file_object:
    lines = file_object.readlines()
pi_string = ''
for line in lines:
    pi_string += line.rstrip()
print(pi_string)
print(len(pi_string))
# 10.1.6 包含一百万位的大型文件
